package cn.com.tuns.assess.qsb.activity;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.*;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import cn.com.tuns.assess.qsb.R;
import cn.com.tuns.assess.qsb.frame.ui.AppManage;
import cn.com.tuns.assess.qsb.frame.ui.BaseActivity;
import cn.com.tuns.assess.qsb.frame.ui.Config;
import cn.com.tuns.assess.qsb.frame.util.AndroidUtils;
import cn.com.tuns.assess.qsb.frame.util.SharedPreferencesUtil;
import cn.com.tuns.assess.qsb.frame.widget.DialogUtil;
import cn.com.tuns.assess.qsb.frame.widget.dialog.BottomDialog;
import cn.com.tuns.assess.qsb.frame.widget.x5web.X5WebView;
import com.tencent.smtt.export.external.interfaces.ConsoleMessage;
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
import com.tencent.smtt.export.external.interfaces.JsResult;
import com.tencent.smtt.sdk.*;
import com.tencent.smtt.utils.TbsLog;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.UUID;

public class BrowserActivity extends BaseActivity {
	/**
	 * 作为一个浏览器的示例展示出来，采用android+web的模式
	 */
	private X5WebView mWebView;
	private ViewGroup mViewParent;
//	private ImageButton mBack;
//	private ImageButton mForward;
//	private ImageButton mExit;
//	private ImageButton mHome;
//	private ImageButton mMore;
	private Button mGo;
	private EditText mUrl;

	private static final String mHomeUrl = "http://192.168.2.58:8000/";
	private static final String TAG = "SdkDemo";
	private static final int MAX_LENGTH = 14;
	private boolean mNeedTestPage = false;

	private final int disable = 120;
	private final int enable = 255;

	private ProgressBar mPageLoadingProgressBar = null;

	//5.0以下使用
	private ValueCallback<Uri> uploadMessage;
	// 5.0及以上使用
	private ValueCallback<Uri[]> uploadMessageAboveL;

	private URL mIntentUrl;

	private static final int PICKER_LEGACY = 3;
	private static final int PICKER = 1;

	//图片
	private final static int FILE_CHOOSER_RESULT_CODE = 128;
	//拍照
	private final static int FILE_CAMERA_RESULT_CODE = 129;
	//拍照图片路径
	private String cameraFielPath;

	private File outputImage;
	private File outputVideo;
	public boolean taken = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.TRANSLUCENT);

		Intent intent = getIntent();
		if (intent != null) {
			try {
				mIntentUrl = new URL(intent.getData().toString());
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (NullPointerException e) {

			} catch (Exception e) {
			}
		}
		//
		try {
			if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 11) {
				getWindow()
						.setFlags(
								android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
								android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
			}
		} catch (Exception e) {
		}

		/*
		 * getWindow().addFlags(
		 * android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN);
		 */
		setContentView(R.layout.activity_main);
		mViewParent = (ViewGroup) findViewById(R.id.webViewLay);

		mTestHandler.sendEmptyMessageDelayed(MSG_INIT_UI, 10);

	}


	private void initProgressBar() {
		mPageLoadingProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
		mPageLoadingProgressBar.setMax(100);
		mPageLoadingProgressBar.setProgressDrawable(this.getResources()
				.getDrawable(R.drawable.color_progressbar));
	}

	private void init() {

		mWebView = findViewById(R.id.webView);

		initProgressBar();

		// webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
		// webSetting.setPreFectch(true);
		long time = System.currentTimeMillis();
		if (mIntentUrl == null) {
			mWebView.loadUrl(Config.getWebHomeUrl());
		} else {
			mWebView.loadUrl(mIntentUrl.toString());
		}
		TbsLog.d("time-cost", "cost time: "
				+ (System.currentTimeMillis() - time));
		CookieSyncManager.createInstance(this);
		CookieSyncManager.getInstance().sync();

		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				// mTestHandler.sendEmptyMessage(MSG_OPEN_TEST_URL);
//				mTestHandler.sendEmptyMessageDelayed(MSG_OPEN_TEST_URL, 5000);// 5s?
//				if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 16)
//					changGoForwardButton(view);
				/* mWebView.showLog("test Log"); */
			}

		});

		mWebView.setWebChromeClient(new WebChromeClient() {

			@Override
			public boolean onJsConfirm(WebView arg0, String arg1, String arg2,
									   JsResult arg3) {
				return super.onJsConfirm(arg0, arg1, arg2, arg3);
			}

			View myVideoView;
			View myNormalView;
			IX5WebChromeClient.CustomViewCallback callback;

			// /////////////////////////////////////////////////////////
			//
			/**
			 * 全屏播放配置
			 */
			@Override
			public void onShowCustomView(View view,
										 IX5WebChromeClient.CustomViewCallback customViewCallback) {
				FrameLayout normalView = (FrameLayout) findViewById(R.id.web_filechooser);
				ViewGroup viewGroup = (ViewGroup) normalView.getParent();
				viewGroup.removeView(normalView);
				viewGroup.addView(view);
				myVideoView = view;
				myNormalView = normalView;
				callback = customViewCallback;
			}

			@Override
			public void onHideCustomView() {
				if (callback != null) {
					callback.onCustomViewHidden();
					callback = null;
				}
				if (myVideoView != null) {
					ViewGroup viewGroup = (ViewGroup) myVideoView.getParent();
					viewGroup.removeView(myVideoView);
					viewGroup.addView(myNormalView);
				}
			}

			@Override
			public boolean onJsAlert(WebView arg0, String arg1, String arg2,
									 JsResult arg3) {
				/**
				 * 这里写入你自定义的window alert
				 */
				return super.onJsAlert(null, arg1, arg2, arg3);
			}


			@Override
			public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
				Log.e("webActivity",consoleMessage.message());
				return super.onConsoleMessage(consoleMessage);
			}


			// For Android  > 4.1.1
			public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
				Log.i("test", "openFileChooser 3");
				uploadMessage = uploadMsg;
				openImageChooserActivity();
			}

			// For Android  >= 5.0
			public boolean onShowFileChooser(WebView webView,
											 ValueCallback<Uri[]> filePathCallback,
											 WebChromeClient.FileChooserParams fileChooserParams) {
				Log.i("test", "openFileChooser 4:" + filePathCallback.toString());
				String[] acceptTypes = fileChooserParams.getAcceptTypes();
				boolean allowMultiple = fileChooserParams.getMode() == android.webkit.WebChromeClient.FileChooserParams.MODE_OPEN_MULTIPLE;
				return startPhotoPickerIntent(filePathCallback, acceptTypes, allowMultiple);
			}
		});

		mWebView.setDownloadListener(new DownloadListener() {

			@Override
			public void onDownloadStart(String arg0, String arg1, String arg2,
										String arg3, long arg4) {
				TbsLog.d(TAG, "url: " + arg0);
				new AlertDialog.Builder(BrowserActivity.this)
						.setTitle("allow to download？")
						.setPositiveButton("yes",
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
														int which) {
										Toast.makeText(
												BrowserActivity.this,
												"fake message: i'll download...",
												1000).show();
									}
								})
						.setNegativeButton("no",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
														int which) {
										// TODO Auto-generated method stub
										Toast.makeText(
												BrowserActivity.this,
												"fake message: refuse download...",
												Toast.LENGTH_SHORT).show();
									}
								})
						.setOnCancelListener(
								new DialogInterface.OnCancelListener() {

									@Override
									public void onCancel(DialogInterface dialog) {
										// TODO Auto-generated method stub
										Toast.makeText(
												BrowserActivity.this,
												"fake message: refuse download...",
												Toast.LENGTH_SHORT).show();
									}
								}).show();
			}
		});

		if(AndroidUtils.isApkInDebug(this)) {
			View view = findViewById(R.id.test);
			view.setVisibility(View.VISIBLE);
			view.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					showTestDialog();
				}
			});
		}

	}

	private void openImageChooserActivity() {
		new BottomDialog(this) {
			@Override
			public void dialogInit() {
				getLayoutInflater().inflate(R.layout.dialog_take_select, getContentlLayout());
				findViewById(R.id.take).setOnClickListener(this);
				findViewById(R.id.photo_album).setOnClickListener(this);
				findViewById(R.id.cancel).setOnClickListener(this);

				setOnCancelListener(new DialogInterface.OnCancelListener() {
					@Override
					public void onCancel(DialogInterface dialogInterface) {
						if (uploadMessageAboveL != null) {
							uploadMessageAboveL.onReceiveValue(null);
							uploadMessageAboveL = null;
						}
						if (uploadMessage != null) {
							uploadMessage.onReceiveValue(null);
							uploadMessage = null;
						}
					}
				});
			}

			@Override
			public void onClick(View view) {
				super.onClick(view);
				//拍照
				if (view.getId() == R.id.take) {
					takeCamera();
				} else //相册
					if (view.getId() == R.id.photo_album) {
						takePhoto();
					} else {
						if (uploadMessageAboveL != null) {
							uploadMessageAboveL.onReceiveValue(null);
							uploadMessageAboveL = null;
						}
						if (uploadMessage != null) {
							uploadMessage.onReceiveValue(null);
							uploadMessage = null;
						}
					}
				dismiss();
			}
		}.show();
	}

	//选择图片
	private void takePhoto() {
		Intent i = new Intent(Intent.ACTION_GET_CONTENT);
		i.addCategory(Intent.CATEGORY_OPENABLE);
		i.setType("image/*");
		startActivityForResult(Intent.createChooser(i, "Image Chooser"), FILE_CHOOSER_RESULT_CODE);
	}

	//拍照
	private void takeCamera() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		if (AndroidUtils.checkFreeSpace(10)) {
			File photoFolder =new File(AppManage.getCameraPath()) ;
			if (!photoFolder.exists()) {
				photoFolder.mkdirs();
			}
			File photoFile = new File(photoFolder, UUID.randomUUID().toString() + ".jpg");
			intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(this, "com.kernal.passportreader.sdk.fileprovider", photoFile));
			cameraFielPath = photoFile.getAbsolutePath();
			startActivityForResult(intent, FILE_CAMERA_RESULT_CODE);
		} else {
			if (uploadMessageAboveL != null) {
				uploadMessageAboveL.onReceiveValue(null);
				uploadMessageAboveL = null;
			}
			if (uploadMessage != null) {
				uploadMessage.onReceiveValue(null);
				uploadMessage = null;
			}
		}
	}

	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	public boolean startPhotoPickerIntent(final ValueCallback<Uri[]> callback, final String[] acceptTypes, final boolean allowMultiple) {
		uploadMessageAboveL = callback;

		BaseActivity activity = (BaseActivity) this;
		activity.setRequestPermissionListener(new RequestPermissionListener() {

			@Override
			public void onRequestPermissionListener(int requestCode, boolean granted) {
				ArrayList<Parcelable> extraIntents = new ArrayList<>();
				if (!needsCameraPermission()) {
					if (acceptsImages(acceptTypes)) {
						Intent photoIntent = getPhotoIntent();
						if (photoIntent != null) {
							extraIntents.add(photoIntent);
						}
					}
					if (acceptsVideo(acceptTypes)) {
						Intent videoIntent = getVideoIntent();
						if (videoIntent != null) {
							extraIntents.add(videoIntent);
						}
					}
				}

				Intent fileSelectionIntent = getFileChooserIntent(acceptTypes, allowMultiple);

				Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
				chooserIntent.putExtra(Intent.EXTRA_INTENT, fileSelectionIntent);
				chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents.toArray(new Parcelable[]{}));

				if (chooserIntent.resolveActivity(BrowserActivity.this.getPackageManager()) != null) {
					BrowserActivity.this.startActivityForResult(chooserIntent, PICKER);
				} else {
					Log.e("WebActivity", "there is no Activity to handle this Intent");
				}
			}
		});
		String[] permissions = {Manifest.permission.CAMERA};
		activity.requestDangerousPermissions(permissions, 103);

		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		TbsLog.d(TAG, "onActivityResult, requestCode:" + requestCode
				+ ",resultCode:" + resultCode);

		if (uploadMessage == null && uploadMessageAboveL == null) {
			return;
		}

		boolean imageTaken = false;
		boolean videoTaken = false;

		taken = false;
		if (outputImage != null && outputImage.length() > 0) {
			imageTaken = true;
			taken = true;
		}
		if (outputVideo != null && outputVideo.length() > 0) {
			videoTaken = true;
			taken = true;
		}

		// based off of which button was pressed, we get an activity result and a file
		// the camera activity doesn't properly return the filename* (I think?) so we use
		// this filename instead
		switch (requestCode) {
			case PICKER:
				if (resultCode != RESULT_OK) {
					if (uploadMessageAboveL != null) {
						uploadMessageAboveL.onReceiveValue(null);
					}
				} else {
					if (imageTaken) {
						uploadMessageAboveL.onReceiveValue(new Uri[]{getOutputUri(outputImage)});
					} else if (videoTaken) {
						uploadMessageAboveL.onReceiveValue(new Uri[]{getOutputUri(outputVideo)});
					} else {
						uploadMessageAboveL.onReceiveValue(this.getSelectedFiles(data, resultCode));
					}
				}
				break;
			case PICKER_LEGACY:
				if (resultCode != RESULT_OK) {
					if (uploadMessage != null) {
						uploadMessage.onReceiveValue(null);
					}
				} else {
					if (imageTaken) {
						uploadMessage.onReceiveValue(getOutputUri(outputImage));
					} else if (videoTaken) {
						uploadMessage.onReceiveValue(getOutputUri(outputVideo));
					} else {
						uploadMessage.onReceiveValue(data.getData());
					}
				}
				break;

		}

		if (outputImage != null && !imageTaken) {
			outputImage.delete();
		}
		if (outputVideo != null && !videoTaken) {
			outputVideo.delete();
		}

		uploadMessage = null;
		uploadMessageAboveL = null;
		outputImage = null;
		outputVideo = null;

	}

	private Uri[] getSelectedFiles(Intent data, int resultCode) {
		if (data == null) {
			return null;
		}

		// we have multiple files selected
		if (data.getClipData() != null) {
			final int numSelectedFiles = data.getClipData().getItemCount();
			Uri[] result = new Uri[numSelectedFiles];
			for (int i = 0; i < numSelectedFiles; i++) {
				result[i] = data.getClipData().getItemAt(i).getUri();
			}
			return result;
		}

		// we have one file selected
		if (data.getData() != null && resultCode == RESULT_OK && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			return android.webkit.WebChromeClient.FileChooserParams.parseResult(resultCode, data);
		}

		return null;
	}

	private enum MimeType {
		DEFAULT("*/*"),
		IMAGE("image"),
		VIDEO("video");

		private final String value;

		MimeType(String value) {
			this.value = value;
		}
	}

	protected boolean needsCameraPermission() {
		boolean needed = false;

		PackageManager packageManager = this.getPackageManager();
		try {
			String[] requestedPermissions = packageManager.getPackageInfo(this.getPackageName(), PackageManager.GET_PERMISSIONS).requestedPermissions;
			if (Arrays.asList(requestedPermissions).contains(Manifest.permission.CAMERA)
					&& ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
				needed = true;
			}
		} catch (PackageManager.NameNotFoundException e) {
			needed = true;
		}

		return needed;
	}

	private Boolean noAcceptTypesSet(String[] types) {
		return types.length == 0 || (types.length == 1 && types[0] != null && types[0].length() == 0);
	}

	private String getMimeTypeFromExtension(String extension) {
		String type = null;
		if (extension != null) {
			type = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
		}
		return type;
	}

	private String[] getAcceptedMimeType(String[] types) {
		if (noAcceptTypesSet(types)) {
			return new String[]{MimeType.DEFAULT.value};
		}
		String[] mimeTypes = new String[types.length];
		for (int i = 0; i < types.length; i++) {
			String t = types[i];
			// convert file extensions to mime types
			if (t.matches("\\.\\w+")) {
				String mimeType = getMimeTypeFromExtension(t.replace(".", ""));
				if (mimeType != null) {
					mimeTypes[i] = mimeType;
				} else {
					mimeTypes[i] = t;
				}
			} else {
				mimeTypes[i] = t;
			}
		}
		return mimeTypes;
	}

	private Boolean arrayContainsString(String[] array, String pattern) {
		for (String content : array) {
			if (content.contains(pattern)) {
				return true;
			}
		}
		return false;
	}

	private Boolean acceptsImages(String[] types) {
		String[] mimeTypes = getAcceptedMimeType(types);
		return arrayContainsString(mimeTypes, MimeType.DEFAULT.value) || arrayContainsString(mimeTypes, MimeType.IMAGE.value);
	}

	private File getCapturedFile(MimeType mimeType) throws IOException {
		String prefix = "";
		String suffix = "";
		String dir = "";

		switch (mimeType) {
			case IMAGE:
				prefix = "image-";
				suffix = ".jpg";
				dir = Environment.DIRECTORY_PICTURES;
				break;
			case VIDEO:
				prefix = "video-";
				suffix = ".mp4";
				dir = Environment.DIRECTORY_MOVIES;
				break;

			default:
				break;
		}

		String filename = prefix + String.valueOf(System.currentTimeMillis()) + suffix;
		File outputFile = null;

		// for versions below 6.0 (23) we use the old File creation & permissions model
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
			// only this Directory works on all tested Android versions
			// ctx.getExternalFilesDir(dir) was failing on Android 5.0 (sdk 21)
			File storageDir = Environment.getExternalStoragePublicDirectory(dir);
			outputFile = new File(storageDir, filename);
		} else {
			File storageDir = this.getExternalFilesDir(null);
			outputFile = File.createTempFile(prefix, suffix, storageDir);
		}

		return outputFile;
	}

	private Uri getOutputUri(File capturedFile) {
		// for versions below 6.0 (23) we use the old File creation & permissions model
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
			return Uri.fromFile(capturedFile);
		}

		// for versions 6.0+ (23) we use the FileProvider to avoid runtime permissions
		String packageName = this.getPackageName();
		return FileProvider.getUriForFile(this, packageName + ".fileprovider", capturedFile);
	}

	private Intent getPhotoIntent() {
		Intent intent = null;

		try {
			outputImage = getCapturedFile(MimeType.IMAGE);
			Uri outputImageUri = getOutputUri(outputImage);
			intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			intent.putExtra(MediaStore.EXTRA_OUTPUT, outputImageUri);
		} catch (IOException | IllegalArgumentException e) {
			Log.e("CREATE FILE", "Error occurred while creating the File");
			e.printStackTrace();
		}

		return intent;
	}

	private Intent getVideoIntent() {
		Intent intent = null;

		try {
			outputVideo = getCapturedFile(MimeType.VIDEO);
			Uri outputVideoUri = getOutputUri(outputVideo);
			intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
			intent.putExtra(MediaStore.EXTRA_OUTPUT, outputVideoUri);
		} catch (IOException | IllegalArgumentException e) {
			Log.e("CREATE FILE", "Error occurred while creating the File");
			e.printStackTrace();
		}

		return intent;
	}

	private Intent getFileChooserIntent(String[] acceptTypes, boolean allowMultiple) {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.addCategory(Intent.CATEGORY_OPENABLE);
		intent.setType(MimeType.DEFAULT.value);
		intent.putExtra(Intent.EXTRA_MIME_TYPES, getAcceptedMimeType(acceptTypes));
		intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, allowMultiple);
		return intent;
	}

	private Boolean acceptsVideo(String types) {
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
			return false;
		}

		String mimeType = types;
		if (types.matches("\\.\\w+")) {
			mimeType = getMimeTypeFromExtension(types.replace(".", ""));
		}
		return mimeType.isEmpty() || mimeType.toLowerCase().contains(MimeType.VIDEO.value);
	}

	private Boolean acceptsVideo(String[] types) {
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
			return false;
		}

		String[] mimeTypes = getAcceptedMimeType(types);
		return arrayContainsString(mimeTypes, MimeType.DEFAULT.value) || arrayContainsString(mimeTypes, MimeType.VIDEO.value);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		if (intent == null || mWebView == null || intent.getData() == null)
			return;
		mWebView.loadUrl(intent.getData().toString());
	}

	@Override
	protected void onDestroy() {
		if (mTestHandler != null)
			mTestHandler.removeCallbacksAndMessages(null);
		if (mWebView != null)
			mWebView.destroy();
		super.onDestroy();
	}

	public static final int MSG_OPEN_TEST_URL = 0;
	public static final int MSG_INIT_UI = 1;
	private final int mUrlStartNum = 0;
	private int mCurrentUrl = mUrlStartNum;
	private Handler mTestHandler = new Handler(Looper.getMainLooper())
	{
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
//			case MSG_OPEN_TEST_URL:
//				if (!mNeedTestPage) {
//					return;
//				}
//
//				String testUrl = "file:///sdcard/outputHtml/html/"
//						+ Integer.toString(mCurrentUrl) + ".html";
//				if (mWebView != null) {
//					mWebView.loadUrl(testUrl);
//				}
//
//				mCurrentUrl++;
//				break;
			case MSG_INIT_UI:
				init();
				break;
			}
			super.handleMessage(msg);
		}
	};


	@Override
	public void onBackPressed() {
		if(mWebView!=null&&mWebView.canGoBack())
		{
			mWebView.goBack();
		}
		else {
			super.onBackPressed();
		}

	}

	private void showTestDialog(){
		String url= SharedPreferencesUtil.read(SharedPreferencesUtil.TestWebUrl);
		if(TextUtils.isEmpty(url))
		{
			url="http://192.168.2.58:8000/login";
		}

		View root=View.inflate(this,R.layout.test_dialog_main,null);
		final EditText editText=root.findViewById(R.id.weburl);
		editText.setText(url);
		View qd=root.findViewById(R.id.text_right);
		View cansel=root.findViewById(R.id.text_left);

		final DialogUtil dialogUtil = new DialogUtil(this, root, DialogUtil.Type.Center, true);
		qd.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String url=editText.getText().toString();
				SharedPreferencesUtil.save(SharedPreferencesUtil.TestWebUrl,url);
				dialogUtil.dismiss();

				mWebView.loadUrl(url);
			}
		});
		cansel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialogUtil.dismiss();
			}
		});
		dialogUtil.show();

	}
}
